#!/bin/bash
#
# Target dist: Debian Squeeze
DIST=precise

rm -rf ${DIST}
cp -r debian ${DIST}

# No systemd
sed -i -e '/dh-systemd/d' ${DIST}/control
sed -i -e '/dh_systemd/d' ${DIST}/rules
rm ${DIST}/kamailio.service

# No mi_xmlrpc module
sed -i -e '/libxmlrpc-c3-dev/d' -e '/^Package: kamailio-xmlrpc-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ mi_xmlrpc[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ mi_xmlrpc/' ${DIST}/rules

# No dnssec module:
sed -i -e '/libval-dev/d' -e '/^Package: kamailio-dnssec-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ dnssec[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ dnssec/' ${DIST}/rules

# use old radius-client-ng
sed -i 's/libradcli-dev/libradiusclient-ng-dev/' ${DIST}/control
sed -i -e '/RADCLI/d' ${DIST}/rules

# use old json
sed -i 's/libjson-c-dev/libjson0-dev/' ${DIST}/control

# no kazoo module
sed -i -e '/librabbitmq-dev/d' -e '/^Package: kamailio-kazoo-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ kazoo[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ kazoo/' ${DIST}/rules

# no cnxcc module
sed -i -e '/^Package: kamailio-cnxcc-modules/,/^$/d' ${DIST}/control
sed -i -e 's/ cnxcc[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ cnxcc/' ${DIST}/rules

# no systemd module
sed -i -e '/libsystemd-dev/d' -e '/^Package: kamailio-systemd-modules/,/^$/d' \
  ${DIST}/control
sed -i -e 's/ systemd[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ systemd/' ${DIST}/rules

# old mysql
sed -i -e 's/default-mysql-client/mysql-client/' \
  -e 's/default-libmysqlclient-dev/libmysqlclient-dev/' ${DIST}/control
echo "kamailio source: build-depends-on-obsolete-package build-depends: libmysqlclient-dev => default-libmysqlclient-dev" >> \
	${DIST}/source.lintian-overrides
echo "kamailio-mysql-modules: depends-on-obsolete-package depends: mysql-client => default-mysql-client" >> \
	${DIST}/kamailio-mysql-modules.lintian-overrides

# no libssl-dev
sed -i -e '/libssl-dev/d' ${DIST}/control

# no rabbitmq
sed -i -e '/librabbitmq-dev/d' \
  -e '/^Package: kamailio-rabbitmq-modules/,/^$/d' ${DIST}/control
sed -i -e 's/ rabbitmq[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ rabbitmq/' ${DIST}/rules

# No geoip2 module
sed -i -e '/libmaxminddb-dev/d' -e '/^Package: kamailio-geoip2-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ geoip2[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ geoip2/' ${DIST}/rules

# No phonenum module
sed -i -e '/libphonenumber-dev/d' -e '/^Package: kamailio-phonenum-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ phonenum[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ phonenum/' ${DIST}/rules

# Mo mongodb
sed -i -e '/libmongoc-dev/d' -e '/^Package: kamailio-mongodb-modules/,/^$/d' \
	${DIST}/control
sed -i -e 's/ mongodb[ ,$]*/ /' ${DIST}/rules
sed -i -e '/^EXTRA_EXCLUDED_MODULES=/s/$/ mongodb/' ${DIST}/rules

wrap-and-sort -sat -d ${DIST}

# clean backports scripts
rm -rf ${DIST}/backports
exit 0
